
class Compiler {
  constructor(vm) {
    this.el = vm.$el
    this.vm = vm
    this.compile(this.el)
  }
  // 编译模板，处理文本节点和元素节点
  compile (el) {
    let childNodes = el.childNodes
    Array.from(childNodes).forEach(node => {
      if (this.isTextNode(node)) {
        this.compileText(node)
      } else if (this.isElementNode(node)) {
        this.compileElement(node)
      }

      // 判断node节点，是否有子节点，如果有子节点，要递归调用compile
      if (node.childNodes && node.childNodes.length) {
        this.compile(node)
      }
    })
  }
  // 编译元素节点，处理指令
  compileElement (node) {
    Array.from(node.attributes).forEach(attr => {
      let attrName = attr.name;
      if (this.isDirective(attrName)) {
        node.removeAttribute(attrName); // 应该删除vue的指令，这样好看点
        attrName = attrName.replace('v-', '');
        let key = attr.value;
        this.update(node, key, attrName)
      }
    })
  }

  update (node, key, attrName) {
    let eventName = '';
    if (attrName.indexOf(':') > -1) {
      eventName = attrName.split(':')[1];
      attrName = attrName.split(':')[0] || 'bind';
    }

    if (attrName.indexOf('@') > -1) {
      eventName = attrName.split('@')[1];
      attrName = 'on';
    }

    let updateFn = this[`${attrName}Updater`];
    updateFn && updateFn.call(this, node, this.vm[key], key, eventName)
  }

  // 处理 v-text 指令
  textUpdater (node, value, key) {
    node.textContent = value
    new Watcher(this.vm, key, (newValue) => {
      node.textContent = newValue
    })
  }

  // 处理 v-model 指令
  modelUpdater (node, value, key) {
    node.value = value
    new Watcher(this.vm, key, (newValue) => {
      node.value = newValue
    });

    // 双向绑定
    node.addEventListener('input', () => {
      this.vm[key] = node.value
    })
  }

  // 处理 v-on 指令
  onUpdater (node, value, key, eventName) {
    node.addEventListener(eventName, value);
  }

  // 处理 v-html 指令
  htmlUpdater (node, value = '', key) {
    node.innerHTML = value;
    new Watcher(this.vm, key, (newValue) => {
      node.innerHTML = newValue
    })
  }

  // 处理 v-if 指令
  ifUpdater (node, value, key) {
    node.style.display = value ? '': 'none';
    new Watcher(this.vm, key, (newValue) => {
      node.style.display = newValue ? '': 'none';
    })
  }

  // 处理 v-bind 指令
  bindUpdater (node, value, key, eventName) {
    node.setAttribute(eventName, value);
    new Watcher(this.vm, key, (newValue) => {
      node.setAttribute(eventName, newValue);
    })
  }

  // 编译文本节点，处理差值表达式
  compileText (node) {
    let reg = /\{\{(.+?)\}\}/
    let value = node.textContent
    if (reg.test(value)) {
      let key = RegExp.$1.trim()
      node.textContent = value.replace(reg, this.vm[key])

      // 创建watcher对象，当数据改变更新视图
      new Watcher(this.vm, key, (newValue) => {
        node.textContent = newValue
      })
    }
  }

  // 判断元素属性是否是指令
  isDirective (attrName) {
    return attrName.startsWith('v-') || attrName.startsWith('@') || attrName.startsWith(':');
  }
  // 判断节点是否是文本节点
  isTextNode (node) {
    return node.nodeType === 3
  }
  // 判断节点是否是元素节点
  isElementNode (node) {
    return node.nodeType === 1
  }
}

window.Compiler = Compiler;

